import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;

import java.awt.Font;

import javax.swing.JTextArea;

import java.awt.Color;

import javax.swing.UIManager;
import javax.swing.JButton;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

import javax.swing.JComboBox;
import javax.swing.JList;
import javax.swing.AbstractListModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JEditorPane;
import javax.swing.JOptionPane;

import java.awt.SystemColor;


public class PrologueGUI extends JFrame {

	private JPanel contentPane;
	public int playerNumber = 0,humNum,botNum,coins,ante,raise;;
	private Table t;
	private JComboBox HumanNumberCBox, BotNumberCBox;
	private JEditorPane CoinsPane, AntePane, RaisePane;
	public NameGUI nameFrame;

	PrologueGUI(Table t) {
		this.t = t;
		setResizable(false);
		setTitle("Poker");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(350, 50, 600, 600);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel WelcomeLabel = new JLabel("Welcome in Poker Game");
		WelcomeLabel.setFont(new Font("Tahoma", Font.PLAIN, 25));
		WelcomeLabel.setBounds(167, 33, 281, 45);
		contentPane.add(WelcomeLabel);
		
		JLabel SelectPlayersLabel = new JLabel("Set options");
		SelectPlayersLabel.setFont(new Font("Tahoma", Font.PLAIN, 17));
		SelectPlayersLabel.setBounds(261, 108, 88, 19);
		contentPane.add(SelectPlayersLabel);
		
		JTextArea VersionTArea = new JTextArea();
		VersionTArea.setFont(new Font("Monospaced", Font.PLAIN, 11));
		VersionTArea.setBackground(UIManager.getColor("Button.background"));
		VersionTArea.setText("2.0 version by Micha\u0142 Nguyen Anh & Pawe\u0142 Olszowski");
		VersionTArea.setBounds(221, 535, 353, 16);
		contentPane.add(VersionTArea);
		
		HumanNumberCBox = new JComboBox();
		HumanNumberCBox.setModel(new DefaultComboBoxModel(new String[] {"1 ", "2 ", "3 ", "4"}));
		HumanNumberCBox.setBounds(167, 188, 59, 19);
		contentPane.add(HumanNumberCBox);
		
		JLabel HumansNumberLabel = new JLabel("Humans number:");
		HumansNumberLabel.setFont(new Font("Tahoma", Font.PLAIN, 15));
		HumansNumberLabel.setBounds(139, 163, 119, 14);
		contentPane.add(HumansNumberLabel);
		
		JLabel BotNumberLabel = new JLabel("Bot number:");
		BotNumberLabel.setFont(new Font("Tahoma", Font.PLAIN, 15));
		BotNumberLabel.setBounds(363, 163, 82, 14);
		contentPane.add(BotNumberLabel);
		
		BotNumberCBox = new JComboBox();
		BotNumberCBox.setModel(new DefaultComboBoxModel(new String[] {"0", "1", "2", "3"}));
		BotNumberCBox.setBounds(373, 187, 60, 20);
		contentPane.add(BotNumberCBox);
		
		JLabel CoinsLabel = new JLabel("Starting coins:");
		CoinsLabel.setFont(new Font("Tahoma", Font.PLAIN, 15));
		CoinsLabel.setBounds(135, 251, 91, 19);
		contentPane.add(CoinsLabel);
		
		CoinsPane = new JEditorPane();
		CoinsPane.setBackground(SystemColor.activeCaption);
		CoinsPane.setText("100");
		CoinsPane.setBounds(251, 250, 106, 20);
		contentPane.add(CoinsPane);
		
		JLabel AnteLabel = new JLabel("Ante:");
		AnteLabel.setFont(new Font("Tahoma", Font.PLAIN, 15));
		AnteLabel.setBounds(191, 295, 35, 14);
		contentPane.add(AnteLabel);
		
		AntePane = new JEditorPane();
		AntePane.setBackground(SystemColor.activeCaption);
		AntePane.setText("10");
		AntePane.setBounds(251, 289, 106, 20);
		contentPane.add(AntePane);
		
		JLabel RaiseLabel = new JLabel("Raise amount:");
		RaiseLabel.setFont(new Font("Tahoma", Font.PLAIN, 15));
		RaiseLabel.setBounds(132, 329, 94, 14);
		contentPane.add(RaiseLabel);
		
		RaisePane = new JEditorPane();
		RaisePane.setBackground(SystemColor.activeCaption);
		RaisePane.setText("10");
		RaisePane.setBounds(251, 329, 106, 20);
		contentPane.add(RaisePane);
		
		JButton PlayButton = new JButton("PLAY!");
		PlayButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				Table t = getTable();
				humNum = HumanNumberCBox.getSelectedIndex() + 1;
				botNum = BotNumberCBox.getSelectedIndex();
				if(humNum + botNum == 1) {
					
							JOptionPane.showMessageDialog(contentPane,
					        "Not enought Players!",
					        "Poker - Error",
					        JOptionPane.ERROR_MESSAGE);
					        return;
				}
				
				if(humNum + botNum > 4) {
					
							JOptionPane.showMessageDialog(contentPane,
					        "Players number higher than 4!",
					        "Poker - Error",
					        JOptionPane.ERROR_MESSAGE);
					        return;
				}
				else {
					try{ coins = Integer.parseInt(CoinsPane.getText()); }
					catch(NumberFormatException ex) {
						
								JOptionPane.showMessageDialog(contentPane,
						        "Your wallet can not carry this value!",
						        "Poker - Error",
						        JOptionPane.ERROR_MESSAGE);
						        return;
					}
					try{ ante = Integer.parseInt(AntePane.getText()); }
					catch(NumberFormatException ex) {
						
								JOptionPane.showMessageDialog(contentPane,
						        "Do you really have '"+AntePane.getText()+"' in your wallet?",
						        "Poker - Error",
						        JOptionPane.ERROR_MESSAGE);
						        return;
					}
					try{ raise = Integer.parseInt(RaisePane.getText()); }
					catch(NumberFormatException ex) {
						
								JOptionPane.showMessageDialog(contentPane,
						        "We play for cash only!",
						        "Poker - Error",
						        JOptionPane.ERROR_MESSAGE);
						        return;
					}
					
					setVisible(false);
					NameGUI nameFrame = new NameGUI(t,humNum);
					nameFrame.setVisible(true);
					NameGUIAccess(nameFrame);
				}
			}
		});
		PlayButton.setBounds(251, 397, 106, 23);
		contentPane.add(PlayButton);
	}
	
	private void NameGUIAccess(NameGUI GUI)
	{
		this.nameFrame=GUI;
	}
	
	private Table getTable() {
		
		return this.t;
	}
}
